package com.jqbd.controller;

import com.jqbd.annotation.Log;
import com.jqbd.common.MyJSON;
import com.jqbd.common.R;
import com.jqbd.pojo.User;
import com.jqbd.service.impl.UserServiceImpl;
import com.jqbd.util.JsonResult;
import com.jqbd.util.OssUtil;
import com.jqbd.vo.LacVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Enzo Cotter on 2022/11/10.
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserServiceImpl userService;

    @Log("分页展示-测试自动写入日志")
    @RequestMapping("/findAll")
    public MyJSON<User> queryAllVaccineVaccineController(@RequestParam Map<String, Object> map) {
        return userService.findAll(map);
    }
    //查询用户职责
    @RequestMapping("/selectnickname")
    public MyJSON<String> selectnickname(){
        return userService.selectnickname();
    }
    @RequestMapping("/deleteByPrimaryKey")
    public R deleteByPrimaryKey(Integer id){
        int i = userService.deleteByPrimaryKey(id);
        return i>0?R.ok():R.error();
    }
    @RequestMapping("/insert")
    public R insert(User user){
        int insert = userService.insertSelective(user);
        return insert>0 ?R.ok():R.error();
    }

//    int insertSelective(User record);
    //查询个人信息
    @RequestMapping("/selectByPrimaryKey")
    public R selectByPrimaryKey(Integer id){
        User user = userService.selectByPrimaryKey(id);

        if (user!=null){
            return R.ok().data("user",user);
        }else{
            return R.error();
        }
    }
    @RequestMapping("/updateByPrimaryKey")
    public R updateByPrimaryKey(User user){
        int i = userService.updateByPrimaryKeySelective(user);
        return i>0?R.ok():R.error();
    }
    @RequestMapping("/load")
    public R load(MultipartFile file) throws IOException {
        String picUrl = OssUtil.uploadFile(file.getInputStream(), file.getOriginalFilename());
        return picUrl != null ? R.ok().data("picUrl",picUrl) : R.error().data("url","图片找不到了,请重新上传");
    }
    @RequestMapping("/deleteList")
    public R deleteList(String str){
        int i=userService.deleteList(str);
        return i>0?R.ok():R.error();
    }
    /**
     * 导出Excel
     * @param ids
     * @param response
     */
    @RequestMapping("writeExcel")
    public R writeExcel(Integer [] ids, HttpServletResponse response){
        //System.out.println(Arrays.toString(ids));
        userService.writeExcel(ids,response);
        return R.ok();
    }

    /**
     * 导入Excel
     * @param file
     * @return
     *
     * LayUI中默认上传文件名为：file
     */
    @RequestMapping("upload")
    public JsonResult upload(MultipartFile file){
        //判断
        if(file != null){
            System.out.println("上传的文件名：" + file.getOriginalFilename());

            userService.save(file);

            JsonResult jsonResult = JsonResult.ok();
            return jsonResult;
        }

        throw new RuntimeException("上传文件不能为空！");
    }
    @RequestMapping("/createdate")
    public R createdate()  {
      Map<Object,Object> map =userService.stat();
        return R.ok().data(map);
    }

}
